package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.wordmemorize.common.aspect.operate.OperateLog;
import com.cskaoyan.wordmemorize.common.aspect.operate.OperateLogConstant;
import com.cskaoyan.wordmemorize.common.constant.ResultCodeEnum;
import com.cskaoyan.wordmemorize.common.exception.BusinessException;
import com.cskaoyan.wordmemorize.converter.AIReadConverter;
import com.cskaoyan.wordmemorize.dao.entity.UserVocAISituationDO;
import com.cskaoyan.wordmemorize.dao.entity.UserVocAISituationOptionDO;
import com.cskaoyan.wordmemorize.dao.mapper.UserVocAISituationMapper;
import com.cskaoyan.wordmemorize.dao.mapper.UserVocAISituationOptionMapper;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.UserVocAISituationDTO;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.UserVocAISituationCommand;
import com.cskaoyan.wordmemorize.request.UserVocAISituationOptionCommand;
import com.cskaoyan.wordmemorize.service.AIReadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

@Service
public class AIReadServiceImpl implements AIReadService {

    @Autowired
    UserVocAISituationMapper userVocAISituationMapper;
    @Autowired
    AIReadConverter aiReadConverter;
    @Autowired
    UserVocAISituationOptionMapper userVocAISituationOptionMapper;

    @Override
    @OperateLog(dateType = "AI模式", operateType = OperateLogConstant.OPERATE_ADD)
    public void addAISituation(UserVocAISituationCommand command) {
        LambdaQueryWrapper<UserVocAISituationDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserVocAISituationDO::getName, command.getName());
        UserVocAISituationDO userVocAISituationDO = userVocAISituationMapper.selectOne(queryWrapper);
        if (userVocAISituationDO != null) {
            throw new BusinessException(ResultCodeEnum.AIRead_Module_EXISTED);
        }
        UserVocAISituationDO userVocAISituation = aiReadConverter.userVocAISituationCommandToDO(command);
        userVocAISituationMapper.insert(userVocAISituation);
        List<UserVocAISituationOptionCommand> options = command.getOptions();
        // 使用新的列表转换方法
        List<UserVocAISituationOptionDO> userVocAISituationOptionDOS =
                aiReadConverter.userVocAISituationOptionCommandToDO(options);

        if (!CollectionUtils.isEmpty(userVocAISituationOptionDOS)) {
            userVocAISituationOptionDOS.forEach(option -> {
                option.setSituationId(userVocAISituation.getId());
                userVocAISituationOptionMapper.insert(option);
            });
        }
    }




    @Override
    @OperateLog(dateType = "AI模式", operateType = OperateLogConstant.OPERATE_UPDATE)
    public void updateAISituation(UserVocAISituationCommand command) {
        UserVocAISituationDO userVocAISituation = aiReadConverter.userVocAISituationCommandToDO(command);
        userVocAISituationMapper.updateById(userVocAISituation);
        List<UserVocAISituationOptionDO> userVocAISituationOptionDOS = aiReadConverter.userVocAISituationOptionCommandToDO(command.getOptions());
        if (!CollectionUtils.isEmpty(userVocAISituationOptionDOS)) {
            userVocAISituationOptionDOS.forEach(option -> {
                option.setSituationId(userVocAISituation.getId());
                userVocAISituationOptionMapper.updateById(option);
            });
        }
    }


    @Override
    public void deleteAISituation(Long id) {
        userVocAISituationOptionMapper.delete(new QueryWrapper<UserVocAISituationOptionDO>().eq("situation_id", id));
        userVocAISituationMapper.deleteById(id);

    }

    @Override
    public void deleteAISituationOption(Long id) {
        userVocAISituationOptionMapper.deleteById(id);
    }

    @Override
    public void batchDeleteAISituationOption(List<Long> ids) {
        LambdaQueryWrapper< UserVocAISituationOptionDO>  queryWrapper = new LambdaQueryWrapper<>();
        ids.forEach(id -> queryWrapper.eq(UserVocAISituationOptionDO::getId,id));
        userVocAISituationOptionMapper.delete(queryWrapper);
        userVocAISituationMapper.deleteBatchIds(ids);
    }

    @Override
    public PageDTO<UserVocAISituationDTO> getAISituationList(PageRequest pageRequest) {
        if (pageRequest.getPageNum() == null) {
            //查询所有ai模式
            List<UserVocAISituationDO> userVocAISituationDOS = userVocAISituationMapper.selectList(null);
            return aiReadConverter.toUserVocAISituationPage(userVocAISituationDOS, (long) userVocAISituationDOS.size());
        }
        //需要分页
        Page<UserVocAISituationDO> page = new Page<>(pageRequest.getPageNum(), pageRequest.getCount());
        LambdaQueryWrapper<UserVocAISituationDO> queryWrapper = new LambdaQueryWrapper<>();
        if (pageRequest.getKeyword() != null) {
            queryWrapper.like(UserVocAISituationDO::getName, pageRequest.getKeyword());

        }
        Page<UserVocAISituationDO> userVocAISituationDOS = userVocAISituationMapper.selectPage(page, queryWrapper);

        userVocAISituationDOS.getRecords().forEach(userVocAISituationDO -> {
            userVocAISituationDO.setOptions(userVocAISituationOptionMapper.selectList(new QueryWrapper<UserVocAISituationOptionDO>().eq("situation_id", userVocAISituationDO.getId())));
        });

        return aiReadConverter.toUserVocAISituationPage(userVocAISituationDOS.getRecords(), userVocAISituationDOS.getTotal());
    }
}
